package com.bdqn.ch02.repositroy;

import com.bdqn.ch02.entity.User;
import com.bdqn.ch02.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@SpringBootTest
@RunWith(SpringRunner.class)
public class UserRepositoryTester {
    @Resource
    private UserRepositroy userRepositroy;

    @Resource
    private UserService userService;

//    @Test
//    public void testInsert(){//测试新增
//        User user=new User("测试用户","123456",);
//        userRepositroy.save(user);
//    }


    @Test
    public void testGet(){//测试按主键查询
        User user=userRepositroy.findById(2L).get();
        System.out.println("usrName:"+user.getUsrName());
        System.out.println("roleName:"+user.getRole().getRoleName());
    }

    @Test
    public void testfindByUsrNameOrUsrPasswordLike(){//测试按用户名模糊查询
        List<User> userList=userRepositroy.findByUsrNameOrUsrPasswordLike("%t%","%1%");
        if(userList!=null){
            for (User user:userList){
                System.out.println("usrName:"+user.getUsrName());
            }
        }
    }

    @Test
    public void testFindUserPage(){
        int page=0,size=2;//分页要素，页数要从0开始
        //控制分页数据的排序，可选择升序降序
        //控制分页的辅助类，可以设置页码，每页的数据条数、排序等。
        Pageable pageable=PageRequest.of(page,size,Sort.by(Sort.Direction.DESC, "usrId"));
        Page<User> userPage=userRepositroy.findUserPage(pageable);
        System.out.println("总记录数："+userPage.getTotalElements());
        System.out.println("总页数："+userPage.getTotalPages());
        System.out.println("当前页数："+userPage.getNumber()+1);
        System.out.println("每页记录数："+userPage.getSize());
        System.out.println("当前页记录数："+userPage.getNumberOfElements());
        for (User user:userPage.getContent()){
            System.out.println("usrId:"+user.getUsrId());
        }
    }

    @Test
    public void testFindPageByMap(){
        int page=0,size=2;
        Sort sort=new Sort(Sort.Direction.DESC,"usrId");
        Pageable pageable=PageRequest.of(page,size,sort);
        Map param=new HashMap();
        param.put("usrName","zf");
        param.put("usrPassword","123456");
        Page<User> userPage=userService.findPageByMap(param,pageable);
        System.out.println("总记录数----->"+userPage.getTotalElements());
        System.out.println("总页数----->"+userPage.getPageable());
        System.out.println("当前页数----->"+userPage.getNumber());
        System.out.println("每页记录数----->"+userPage.getSize());
        System.out.println("当前记录----->");
        for (User user:userPage.getContent()){
            System.out.println("usrId:"+user.getUsrId());
        }
    }

//    @Test
//   public void testGetUserInfo(){
//        UserInfo userInfo=userRepositroy.getUserInfo(2L);
//        System.out.println("usrName:"+userInfo.getUsrName());
//        System.out.println("roleName:"+userInfo.getRoleName());
//    }
}
